package com.red.wine.ui;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.google.json.Gson;
import com.google.json.JsonParseException;
import com.red.wine.R;
import com.red.wine.WineApp;
import com.red.wine.adapter.ImageAdapter;
import com.red.wine.adapter.TasteAdapter;
import com.red.wine.model.DataInfo;
import com.red.wine.model.DetailData;
import com.red.wine.model.DetailInfo;
import com.red.wine.model.DetalRequest;
import com.red.wine.model.Headers;
import com.red.wine.model.IdData;
import com.red.wine.model.ReturnInfo;
import com.red.wine.model.TasteBySql;
import com.red.wine.model.TasteInfo;
import com.red.wine.model.TasteRequest;
import com.red.wine.model.TasteReturnData;
import com.red.wine.model.TopInfo;
import com.red.wine.sql.HisSqlHelper;
import com.red.wine.util.IntentUtil;
import com.red.wine.util.PrefsUtils;
import com.red.wine.util.StringUtil;
import com.red.wine.view.MyScrollLayout;
import com.red.wine.webservice.ServiceAsynTask;
import com.red.wine.webservice.ServiceHelper;
import com.red.wine.webservice.ServiceHost;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.provider.MediaStore.Images.Media;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class TasteSecondActivity extends BaseActivity {
	private ImageView share;
	private PopupWindow popuWindow;
	private View popuView;
	private Context context;
	private ImageView back;
	private HisSqlHelper hisSqlHelper;
	boolean hashi = false;
	FrameLayout scrollayout;
	TasteReturnData data;
	private int index = 0;
	boolean fromsql = false;
	private List<TasteInfo> info = null;
	TasteBySql fromsqlinfo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.taset_second_layout);
		context = this;
		 
		hisSqlHelper = new HisSqlHelper();
		hisSqlHelper.open(WineApp.getInstance().getSqlHelper());
		back = (ImageView) findViewById(R.id.taset_second_back);
		scrollayout = (FrameLayout) findViewById(R.id.content_layout_info);
		up = (ImageView) findViewById(R.id.taset_second_next1);
		next = (ImageView) findViewById(R.id.taset_second_next);
		seloc = (ImageView) findViewById(R.id.taset_second_shoucang);
		DisplayMetrics metrics = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay().getMetrics(metrics);
		Bundle bundle = getIntent().getExtras();
		initWidget();
		if (bundle != null) {
			index = bundle.getInt("index");
			data = (TasteReturnData) bundle.get("info");
			fromsql = bundle.getBoolean("fromsql");
			fromsqlinfo= (TasteBySql) bundle.get("tasetinfo");
		}
		back.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});
		Log.e("TAG", "index: = " + index);
		
		if(data != null && data.getItems() != null && data.getItems().size() > 0){
			info = data.getItems();
		}
		if(!fromsql){
			List<TasteBySql> infos = hisSqlHelper.getClientInfo();
			if(infos != null && infos.size() >0){
				for(int i=0; i<infos.size(); i++){
					if(infos.get(i).getId().equals(data.getItems().get(index).getId())){
						hashi = true;
						break;
					}else{
						hashi = false;
					}
				}
			}
		}
		if(fromsql){
			seloc.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.taste_second_shoucang_back_1));
			if(fromsqlinfo != null){
				if(fromsqlinfo.getType().equals("1")){
					View view = LayoutInflater.from(context).inflate(R.layout.second_fram_layout, null);
					TextView title = (TextView) view.findViewById(R.id.taste_detail_title);
					TextView author = (TextView) view.findViewById(R.id.taste_detail_athor);
					TextView content1 = (TextView) view.findViewById(R.id.taste_detail_content);
					TextView content2 = (TextView) view.findViewById(R.id.taste_detail_content_bottom);
					ImageView imgImageView = (ImageView) view.findViewById(R.id.taste_detail_img);
					if(!StringUtil.isEmptyOrNull(fromsqlinfo.getThumbnail_m())){
						imgImageView.setImageBitmap(BitmapFactory.decodeFile(fromsqlinfo.getThumbnail_m()));
					}
					hideLoading();
					title.setText(!StringUtil.isEmptyOrNull(fromsqlinfo.getTitle()) ? fromsqlinfo.getTitle() : "");
					author.setText(!StringUtil.isEmptyOrNull(fromsqlinfo.getAuthor()) ? "文/"+fromsqlinfo.getAuthor() : "");
					if(fromsqlinfo.getContent().length() > 240){
						content1.setText(!StringUtil.isEmptyOrNull(fromsqlinfo.getContent()) ? fromsqlinfo.getContent().substring(0, 240) : "");
						content2.setText(!StringUtil.isEmptyOrNull(fromsqlinfo.getContent()) ? fromsqlinfo.getContent().substring(240, fromsqlinfo.getContent().length()) : "");
					}else{
						content1.setText(!StringUtil.isEmptyOrNull(fromsqlinfo.getContent()) ? fromsqlinfo.getContent() : "");
					}
					view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
					scrollayout.addView(view);
				}else if(fromsqlinfo.getType().equals("2")){
					View view = LayoutInflater.from(context).inflate(R.layout.second_fran_latout, null);
					ImageView imgImageView = (ImageView) view.findViewById(R.id.second_taste_img_s);
					imgImageView.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
					showLoading();
					if(!StringUtil.isEmptyOrNull(fromsqlinfo.getThumb())){
						imgImageView.setImageBitmap(BitmapFactory.decodeFile(fromsqlinfo.getThumb()));
					}
					hideLoading();
					view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
					scrollayout.addView(view);
				}
			}
		}
		if(info != null ){
			if(info.get(index).getType().equals("1")){
				View view = LayoutInflater.from(context).inflate(R.layout.second_fram_layout, null);
				TextView title = (TextView) view.findViewById(R.id.taste_detail_title);
				TextView author = (TextView) view.findViewById(R.id.taste_detail_athor);
				TextView content1 = (TextView) view.findViewById(R.id.taste_detail_content);
				TextView content2 = (TextView) view.findViewById(R.id.taste_detail_content_bottom);
				ImageView imgImageView = (ImageView) view.findViewById(R.id.taste_detail_img);
				showLoading();
				Bitmap bitmap = downImg(info.get(index).getThumbnail_m().get(0));
				imgImageView.setImageBitmap(bitmap);
				hideLoading();
				title.setText(!StringUtil.isEmptyOrNull(info.get(index).getTitle()) ? info.get(index).getTitle() : "");
				author.setText(!StringUtil.isEmptyOrNull(info.get(index).getAuthor()) ? "文/"+info.get(index).getAuthor() : "");
				if(info.get(index).getContent().length() > 240){
					content1.setText(!StringUtil.isEmptyOrNull(info.get(index).getContent()) ? info.get(index).getContent().substring(0, 240) : "");
					content2.setText(!StringUtil.isEmptyOrNull(info.get(index).getContent()) ? info.get(index).getContent().substring(240, info.get(index).getContent().length()) : "");
				}else{
					content1.setText(!StringUtil.isEmptyOrNull(info.get(index).getContent()) ? info.get(index).getContent() : "");
				}
				view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
				scrollayout.addView(view);
			}else if(info.get(index).getType().equals("2")){
				View view = LayoutInflater.from(context).inflate(R.layout.second_fran_latout, null);
				ImageView imgImageView = (ImageView) view.findViewById(R.id.second_taste_img_s);
				imgImageView.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
				showLoading();
				Bitmap bitmap = downImg(info.get(index).getThumb().get(0));
				imgImageView.setImageBitmap(bitmap);
				hideLoading();
				view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
				scrollayout.addView(view);
			}
		}
		
		
		up.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!fromsql){
					showLoading();
					if(index == 0){
						Toast.makeText(context, "当前是第一页", Toast.LENGTH_SHORT).show();
					}else{
						index--;
						List<TasteBySql> infos = hisSqlHelper.getClientInfo();
						if(infos != null && infos.size() >0){
							for(int i=0; i<infos.size(); i++){
								if(infos.get(i).getId().equals(data.getItems().get(index).getId())){
									hashi = true;
									break;
								}else{
									hashi = false;
								}
							}
						}
						if(hashi){
							seloc.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.taste_second_shoucang_back_1));
						}else{
							seloc.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.taste_second_shoucang_back));
						}
						if(info != null && info.get(index) != null){
							if(scrollayout.getChildCount() > 0){
								scrollayout.removeAllViews();
							}
							if(info.get(index).getType().equals("1")){
								View view = LayoutInflater.from(context).inflate(R.layout.second_fram_layout, null);
								TextView title = (TextView) view.findViewById(R.id.taste_detail_title);
								TextView author = (TextView) view.findViewById(R.id.taste_detail_athor);
								TextView content1 = (TextView) view.findViewById(R.id.taste_detail_content);
								TextView content2 = (TextView) view.findViewById(R.id.taste_detail_content_bottom);
								ImageView imgImageView = (ImageView) view.findViewById(R.id.taste_detail_img);
								Bitmap bitmap = downImg(info.get(index).getThumbnail_m().get(0));
								imgImageView.setImageBitmap(bitmap);
								title.setText(!StringUtil.isEmptyOrNull(info.get(index).getTitle()) ? info.get(index).getTitle() : "");
								author.setText(!StringUtil.isEmptyOrNull(info.get(index).getAuthor()) ? "文/"+info.get(index).getAuthor() : "");
								if(info.get(index).getContent().length() > 240){
									content1.setText(!StringUtil.isEmptyOrNull(info.get(index).getContent()) ? info.get(index).getContent().substring(0, 240) : "");
									content2.setText(!StringUtil.isEmptyOrNull(info.get(index).getContent()) ? info.get(index).getContent().substring(240, info.get(index).getContent().length()) : "");
								}else{
									content1.setText(!StringUtil.isEmptyOrNull(info.get(index).getContent()) ? info.get(index).getContent() : "");
								}
								view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
								scrollayout.addView(view);
							}else if(info.get(index).getType().equals("2")){
								View view = LayoutInflater.from(context).inflate(R.layout.second_fran_latout, null);
								ImageView imgImageView = (ImageView) view.findViewById(R.id.second_taste_img_s);
								Bitmap bitmap = downImg(info.get(index).getThumb().get(0));
								imgImageView.setImageBitmap(bitmap);
								view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
								scrollayout.addView(view);
							}
						}else{
							Toast.makeText(context, "获取内容失败", Toast.LENGTH_SHORT).show();
						}
					}
					hideLoading();
				}else{
					if(hisSqlHelper != null && hisSqlHelper.getCount() > 0){
					if(index == 0){
						Toast.makeText(context, "当前是第一页", Toast.LENGTH_SHORT).show();
					}else{
						index--;
						List<TasteBySql> infos = hisSqlHelper.getClientInfo();
						if(infos != null && infos.get(index) != null){
							if(scrollayout.getChildCount() > 0){
								scrollayout.removeAllViews();
							}
							if(infos.get(index).getType().equals("1")){
								View view = LayoutInflater.from(context).inflate(R.layout.second_fram_layout, null);
								TextView title = (TextView) view.findViewById(R.id.taste_detail_title);
								TextView author = (TextView) view.findViewById(R.id.taste_detail_athor);
								TextView content1 = (TextView) view.findViewById(R.id.taste_detail_content);
								TextView content2 = (TextView) view.findViewById(R.id.taste_detail_content_bottom);
								ImageView imgImageView = (ImageView) view.findViewById(R.id.taste_detail_img);
								if(!StringUtil.isEmptyOrNull(fromsqlinfo.getThumbnail_m())){
									imgImageView.setImageBitmap(BitmapFactory.decodeFile(fromsqlinfo.getThumbnail_m()));
								}
								title.setText(!StringUtil.isEmptyOrNull(infos.get(index).getTitle()) ? infos.get(index).getTitle() : "");
								author.setText(!StringUtil.isEmptyOrNull(infos.get(index).getAuthor()) ? "文/"+infos.get(index).getAuthor() : "");
								if(infos.get(index).getContent().length() > 240){
									content1.setText(!StringUtil.isEmptyOrNull(infos.get(index).getContent()) ? infos.get(index).getContent().substring(0, 240) : "");
									content2.setText(!StringUtil.isEmptyOrNull(infos.get(index).getContent()) ? infos.get(index).getContent().substring(240, infos.get(index).getContent().length()) : "");
								}else{
									content1.setText(!StringUtil.isEmptyOrNull(infos.get(index).getContent()) ? infos.get(index).getContent() : "");
								}
								view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
								scrollayout.addView(view);
							}else if(info.get(index).getType().equals("2")){
								View view = LayoutInflater.from(context).inflate(R.layout.second_fran_latout, null);
								ImageView imgImageView = (ImageView) view.findViewById(R.id.second_taste_img_s);
								if(!StringUtil.isEmptyOrNull(infos.get(index).getThumb())){
									imgImageView.setImageBitmap(BitmapFactory.decodeFile(infos.get(index).getThumb()));
								}
								view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
								scrollayout.addView(view);
							}
						}
					}
					}
				}
			}
			
		});
		next.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(!fromsql){
					showLoading();
					if(index == info.size() - 1){
						Toast.makeText(context, "当前是最后一页", Toast.LENGTH_SHORT).show();
					}else{
						index++;
						List<TasteBySql> infos = hisSqlHelper.getClientInfo();
						if(infos != null && infos.size() >0){
							for(int i=0; i<infos.size(); i++){
								if(infos.get(i).getId().equals(data.getItems().get(index).getId())){
									hashi = true;
									break;
								}else{
									hashi = false;
								}
							}
						}
						if(hashi){
							seloc.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.taste_second_shoucang_back_1));
						}else{
							seloc.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.taste_second_shoucang_back));
						}
						if(info != null && info.get(index) != null){
							if(scrollayout.getChildCount() > 0){
								scrollayout.removeAllViews();
							}
							if(info.get(index).getType().equals("1")){
								View view = LayoutInflater.from(context).inflate(R.layout.second_fram_layout, null);
								TextView title = (TextView) view.findViewById(R.id.taste_detail_title);
								TextView author = (TextView) view.findViewById(R.id.taste_detail_athor);
								TextView content1 = (TextView) view.findViewById(R.id.taste_detail_content);
								TextView content2 = (TextView) view.findViewById(R.id.taste_detail_content_bottom);
								ImageView imgImageView = (ImageView) view.findViewById(R.id.taste_detail_img);
								Bitmap bitmap = downImg(info.get(index).getThumbnail_m().get(0));
								imgImageView.setImageBitmap(bitmap);
								
								title.setText(!StringUtil.isEmptyOrNull(info.get(index).getTitle()) ? info.get(index).getTitle() : "");
								author.setText(!StringUtil.isEmptyOrNull(info.get(index).getAuthor()) ? "文/"+info.get(index).getAuthor() : "");
								if(info.get(index).getContent().length() > 240){
									content1.setText(!StringUtil.isEmptyOrNull(info.get(index).getContent()) ? info.get(index).getContent().substring(0, 240) : "");
									content2.setText(!StringUtil.isEmptyOrNull(info.get(index).getContent()) ? info.get(index).getContent().substring(240, info.get(index).getContent().length()) : "");
								}else{
									content1.setText(!StringUtil.isEmptyOrNull(info.get(index).getContent()) ? info.get(index).getContent() : "");
								}
								view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_in));
								scrollayout.addView(view);
							}else if(info.get(index).getType().equals("2")){
								View view = LayoutInflater.from(context).inflate(R.layout.second_fran_latout, null);
								ImageView imgImageView = (ImageView) view.findViewById(R.id.second_taste_img_s);
								Bitmap bitmap = downImg(info.get(index).getThumb().get(0));
								imgImageView.setImageBitmap(bitmap);
								view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_in));
								scrollayout.addView(view);
							}
						}else{
							Toast.makeText(context, "获取内容失败", Toast.LENGTH_SHORT).show();
						}
					}
					hideLoading();
				}else{
					if(hisSqlHelper != null && hisSqlHelper.getCount() > 0){
					if(index == hisSqlHelper.getCount() - 1){
						Toast.makeText(context, "当前是最后一页", Toast.LENGTH_SHORT).show();
					}else{
						index++;
						List<TasteBySql> infos = hisSqlHelper.getClientInfo();
						if(infos != null && infos.get(index) != null){
							if(scrollayout.getChildCount() > 0){
								scrollayout.removeAllViews();
							}
							if(infos.get(index).getType().equals("1")){
								View view = LayoutInflater.from(context).inflate(R.layout.second_fram_layout, null);
								TextView title = (TextView) view.findViewById(R.id.taste_detail_title);
								TextView author = (TextView) view.findViewById(R.id.taste_detail_athor);
								TextView content1 = (TextView) view.findViewById(R.id.taste_detail_content);
								TextView content2 = (TextView) view.findViewById(R.id.taste_detail_content_bottom);
								ImageView imgImageView = (ImageView) view.findViewById(R.id.taste_detail_img);
								if(!StringUtil.isEmptyOrNull(fromsqlinfo.getThumbnail_m())){
									imgImageView.setImageBitmap(BitmapFactory.decodeFile(fromsqlinfo.getThumbnail_m()));
								}
								title.setText(!StringUtil.isEmptyOrNull(infos.get(index).getTitle()) ? infos.get(index).getTitle() : "");
								author.setText(!StringUtil.isEmptyOrNull(infos.get(index).getAuthor()) ? "文/"+infos.get(index).getAuthor() : "");
								if(infos.get(index).getContent().length() > 240){
									content1.setText(!StringUtil.isEmptyOrNull(infos.get(index).getContent()) ? infos.get(index).getContent().substring(0, 240) : "");
									content2.setText(!StringUtil.isEmptyOrNull(infos.get(index).getContent()) ? infos.get(index).getContent().substring(240, infos.get(index).getContent().length()) : "");
								}else{
									content1.setText(!StringUtil.isEmptyOrNull(infos.get(index).getContent()) ? infos.get(index).getContent() : "");
								}
								view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
								scrollayout.addView(view);
							}else if(info.get(index).getType().equals("2")){
								View view = LayoutInflater.from(context).inflate(R.layout.second_fran_latout, null);
								ImageView imgImageView = (ImageView) view.findViewById(R.id.second_taste_img_s);
								if(!StringUtil.isEmptyOrNull(infos.get(index).getThumb())){
									imgImageView.setImageBitmap(BitmapFactory.decodeFile(infos.get(index).getThumb()));
								}
								view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
								scrollayout.addView(view);
							}
						}
					}
					}
				}
			}
		});
		seloc.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				showLoading();
				if(fromsql){
					Toast.makeText(context, "已经收藏", Toast.LENGTH_SHORT).show();
				}else{
//				if(!StringUtil.isEmptyOrNull(PrefsUtils.getSid())){
					List<TasteBySql> infos = hisSqlHelper.getClientInfo();
					if(infos != null && infos.size() >0){
						for(int i=0; i<infos.size(); i++){
							if(infos.get(i).getId().equals(data.getItems().get(index).getId())){
								hashi = true;
								break;
							}else{
								hashi = false;
							}
						}
					}
					
					try {
						
						if (hashi) {
							Toast.makeText(context, "已经收藏", Toast.LENGTH_SHORT).show();
						}else{
							count = hisSqlHelper.getCount();
							
							if(data.getItems() != null && data.getItems().get(index) != null && data.getItems().get(index).getThumbnail_640() != null &&  !StringUtil.isEmptyOrNull(data.getItems().get(index).getThumbnail_640().get(0))){
								String thum640 = data.getItems().get(index).getThumbnail_640().get(0);
								thum_640 = saveCamera(downImg(thum640), false);
							}
							
							if(data.getItems() != null && !StringUtil.isEmptyOrNull(data.getItems().get(index).getThumbnail_64().get(0))){
								String thum64 = data.getItems().get(index).getThumbnail_64().get(0);
								thum_64 = saveCamera(downImg(thum64), false);
							}
						
							if(data.getItems() != null && !StringUtil.isEmptyOrNull(data.getItems().get(index).getThumbnail_m().get(0))){
								String thumm = data.getItems().get(index).getThumbnail_m().get(0);
								thum_m = saveCamera(downImg(thumm), false);
							}
							
							if(data.getItems() != null && !StringUtil.isEmptyOrNull(data.getItems().get(index).getThumb().get(0))){
								String thum = data.getItems().get(index).getThumb().get(0);
								thum_s = saveCamera(downImg(thum), false);
							}
							
							hisSqlHelper.addClientInfo(data.getItems().get(index), thum_640, thum_64, thum_m, thum_s);
							int c = hisSqlHelper.getCount();
							if(c <= count){
								Toast.makeText(context, "收藏失败", Toast.LENGTH_SHORT).show();
							}else{
								seloc.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.taste_second_shoucang_back_1));
								Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
						Toast.makeText(context, "收藏失败", Toast.LENGTH_SHORT).show();
					}
					
	//				}else{
//					Toast.makeText(context, "请先登录", Toast.LENGTH_SHORT).show();
//					IntentUtil.redirectToNext(context, LoginActivity.class);
//					finish();
//				}
					hideLoading();
				}
			}
		});

	}
	int count = 0;
	private String thum_640, thum_64, thum_m, thum_s;
	private ImageView up, next, seloc;
	@Override
	protected void onResume() {
		
		super.onResume();
	}
	 

	private Bitmap downImg(String imgurl) {
		// 启动一个后台线程
		// 这里下载数据
		Bitmap downBItmap = null;
		try {
			URL url = new URL(imgurl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			downBItmap = BitmapFactory.decodeStream(is);
			is.close();
			conn.disconnect();
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return downBItmap;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		finish();
		return super.onKeyDown(keyCode, event);
	}

	private void initWidget() {
		share = (ImageView) findViewById(R.id.taset_second_shareg);
		popuView = LayoutInflater.from(context).inflate(R.layout.share_popu,
				null);
		popuWindow = new PopupWindow(popuView,
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		popuView.setFocusableInTouchMode(true);
		popuWindow.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(Color.HSVToColor(0x11,
				new float[] { 00, 00, 0xff }));
		popuWindow.setBackgroundDrawable(dw);
		popuWindow.setOutsideTouchable(true);

		share.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				popuWindow.showAtLocation(v, Gravity.RIGHT | Gravity.BOTTOM,
						20, 40);

			}
		});
	}
	
	Uri imgUri;
	private String saveCamera(Bitmap theBmp, boolean recycle) {
		byte[] data = null;
		try {
			ByteArrayOutputStream bytes = new ByteArrayOutputStream();
			theBmp.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
			data = bytes.toByteArray();
			bytes.close();
			if (recycle)
				theBmp.recycle();
		} catch (Exception e) {
			// Log.e("imag", "保存图片异常");
			if (theBmp != null) {
				theBmp.recycle();
			}
			e.printStackTrace();
		}
		theBmp = null;
		// make URI to save image
		SimpleDateFormat timeStampFormat = new SimpleDateFormat(
				"yyyyMMddhh.mm.ss");
		String filename = timeStampFormat.format(new Date());
		filename = filename + ".jpg";
		ContentValues values = new ContentValues();
		values.put(Media.TITLE, filename);
		values.put(Media.DISPLAY_NAME, filename);
		values.put(Media.DESCRIPTION, "Image capture by camera");
		values.put(Media.MIME_TYPE, "image/jpeg");

		String directory = WineApp.getInstance().getFilePath();
		String filepath = directory + "/" + filename;
		values.put("_data", filepath);

		// Log.e("iamgeURL", "befor imageUri");
		imgUri = getContentResolver()
				.insert(Media.EXTERNAL_CONTENT_URI, values);
		// Log.e("iamgeURL", "after imageUri");

		try {
			OutputStream filoutputStream = getContentResolver()
					.openOutputStream(imgUri);
			filoutputStream.write(data);
			filoutputStream.flush();
			filoutputStream.close();

		} catch (Exception ex) {
			// Log.e("iamgeURL", "imagURL is error");
			imgUri = null;
			Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
		}
		data = null;
		System.gc();
		return filepath;
	}
}
